<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/8/2
 * Time: 15:29
 */

namespace app\home\model;


use think\Model;
use think\Db;
class InstitutionalActivities extends Model
{
    /**
     * 获取某个机构的活动总时长&总次数
     * @param $mechanism_id 机构ID
     */
    public function activityTime($mechanism_id)
    {
        $duration = $this->field('sum(activity_time) as activityTime, count(id) as num')
            ->where('activity_mechanism_id', $mechanism_id)
            ->where('activity_sh', 1)
            ->select();
        return $duration->toArray();
    }
    
    /**
     * 获取该机构的最热活动的次数&最热活动ID
     * @return int|string 机构ID
     */
    public function bestActivity($mechanism_id)
    {
        $bestActivity = $this->field('count(activity_id) as maxNum, activity_id')
            ->where('activity_mechanism_id', $mechanism_id)
            ->where('activity_sh', 1)
            ->group('activity_id')
            ->order('maxNum desc')
            ->limit(1)
            ->select();
        return $bestActivity->toArray();
    }
    
    /**
     * 获取某个机构发布的所有活动
     * @param $mechanism_id  机构ID
     * @param string $field  需要查询的字段
     * @param $page  当前页码
     */
    public function getActivitys($mechanism_id, $field = '*', $page,$type=1,$keyword='',$pageSize=10){
        $pageNum = $page - 1;
        if($type==1){
            $lists = $this->where('activity_mechanism_id', $mechanism_id)
                ->where('activity_sh', 1)
                ->where('is_show', 1)
                ->field($field)
                ->order('create_time desc')
                ->limit($pageNum * $pageSize, $pageSize)
                ->select();
        }else{
            $where['activity_name'] = array('like',"%$keyword%");
            $lists = $this->where($where)
                ->where('activity_sh', 1)
                ->where('is_show', 1)
                ->field($field)
                ->order('create_time desc')
                ->limit($pageNum * $pageSize, $pageSize)
                ->select();
            foreach ($lists as $kk=>&$vv){

                $activity_name =Db::table('up_activity')->where('id','IN',trim($vv['activity_id'],','))->column('activity_name');
                $vv['activity_names'] = implode(',',$activity_name);
            }
        }

        return $lists;
    }
    
    /**
     * 获取总的活动时长&总的活动次数
     * @return array
     */
    public function totals(){
        $total = $this->field('sum(activity_time) as totalTime, count(id) as totalNum')
            ->where('activity_sh', 1)
            ->where('is_show', 1)
            ->select();
        return $total->toArray();
    }

    /***
     * @param $where
     * @param $order
     * @param $page
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function selectList($where,$order,$page,$field='*',$type=1){
        if($type==1){
            $order_s  = 'activity_num '."$order";
        }else{
            $order_s  = 'is_gq asc,activity_start_time asc';
        }
        $pageNum = $page - 1;
        $res = $this->alias('a')
            ->where($where)
            ->order($order_s)
            ->limit($pageNum * 5, 5)
            ->field($field)
            ->select();
        foreach ($res as $key=>&$vo){
            $activity_name =Db::table('up_activity')->where('id','IN',trim($vo['activity_id'],','))->column('activity_name');
            $vo['activity_names'] = implode(',',$activity_name);
        }
        return $res;
    }

    /***
     * @param $id
     * @param string $field
     * @return array|false|\PDOStatement|string|Model
     * 获取活动单条数据
     */
    public function getOneActivity($id){
        $where['a.id'] = $id;
        $res = $this->alias('a')
                ->join('up_mechanism um','um.mechanism_id = a.activity_mechanism_id','left')
                ->where($where)->field('a.*,um.mechanism_name')->find();
      $activity_name =Db::table('up_activity')->where('id','IN',trim($res['activity_id'],','))->column('activity_name');
      $res['name'] = implode(',',$activity_name);
      return $res;
    }

    /**
     * @param $where
     * @return array|false|\PDOStatement|string|Model
     * 获取单条数据
     */
    public function getInfoList($where){
        $res = $this
            ->where($where)->find();
        return $res;
    }

}